package vip.xiaonuo.smzq.core.tools.file;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.URLUtil;
import org.apache.tika.Tika;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;


import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

/**
 * @author sqq
 * @title PathUtils
 * @date 2024/03/24 16:01
 * @description 路径管理类
 */
public class PathUtils {

    /**
     * @描述 获取当前项目路径
     * @日期 2023/03/24 16:04
     * @作者 sqq
     **/
    public static String getPath() {

        ApplicationHome home = new ApplicationHome(PathUtils.class);
        File jarFile = home.getSource();
        String path = jarFile.getAbsolutePath();
        try {
            path = URLDecoder.decode(path, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        // 调试时，出现路径错误问题， 这里添加判断
        if (FileUtil.exist(path + File.separator + "sqlite")) {
            return path;
        } else {
            String strPath = jarFile.getParentFile().toString();        //用这种方式来获取路径在Windows和Linux下均适用
            //在开发模式下上面这行代码获取到的是target一级文件夹，没有到class，会导致运行错误，所以需要在strPath后面手动增加“class一级目录”
            File folderSQLite = new File(strPath + File.separator + "sqlite");
            if (!folderSQLite.exists()) {
                strPath += File.separator + "classes";
            }
            return strPath;
        }
    }



    /**
     * @描述 获取sqlite 根目录
     * @日期 2023/03/24 16:04
     * @作者 sqq
     **/
    public static String getUpLoadPath(String path) {
        File file = new File(path + File.separator + "upload" + File.separator + DateUtil.format(new Date(), "YYYYMMdd"));
        if (!file.exists()) {
            file.mkdir();
        }
        return file.getPath();
    }

    /**
     * 文件预览，根据文件路径获取文件
     */

    public static void singleFileShow(String filePath, HttpServletResponse response) {
        try {
            filePath = java.net.URLDecoder.decode(filePath, "UTF-8");
            File file = new File(filePath);
            //输入流
            InputStream fis = new FileInputStream(file);
            String type = "";
            switch (FileUtil.extName(filePath).toLowerCase()) {
                case "xlsx":
                case "xls":
                    type = "application/vnd.ms-excel";
                    break;
                case "doc":
                case "docx":
                    type = "application/msword";
                    break;
                case "pdf":
                    type = "application/pdf";
                    break;
                case "img ":
                    type = "application/x-img";
                    break;
                case "jpeg":
                case "jpg":
                    type = "image/jpeg";
                    break;
                case "png":
                    type = "image/png";
                    break;
                case "mdb":
                    type = "application/x-mdb";
                    break;
                case "txt":
                    type = "text/plain";
                    break;
                case "zip":
                    type = "application/zip";
                    break;
                case "csv":
                    type = "text/csv";
                    break;

            }
            if (type.length() > 0) {
                response.setContentType(type);
            } else {
                response.setContentType("image/" + FileUtil.extName(filePath));
            }
            response.setCharacterEncoding("UTF-8");
            // 数据输出
            OutputStream outputStream = response.getOutputStream();// 打开流
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes)) != -1) {
                outputStream.write(bytes, 0, len);
            }
            // 冲刷及关闭
            fis.close();
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        System.out.println(getPath());

    }

    /**
     * 获取年报附件模版路径
     * @param nbfbType
     * @return
     */
    public static String getNbfbPath(Integer nbfbType) {
        String path = "";
        switch (nbfbType) {
            case 1:  //年报附表1
//                path = getPath2() + File.separator + "nbfbExportTemplates" + File.separator + "nbfbOne.xlsx";
                path ="nbfbExportTemplates" + File.separator + "nbfbOne.xlsx";
                break;
            case 2: //年报附表2
                path = "nbfbExportTemplates" + File.separator + "nbfbTwo.xlsx";
                break;
            case 3: //年报附表3
                path = "nbfbExportTemplates" + File.separator + "nbfbThree.xlsx";
                break;
            case 4: //年报附表4
                path = "nbfbExportTemplates" + File.separator + "nbfbFore.xlsx";
                break;
            case 5: //年报附表5
                path =  "nbfbExportTemplates" + File.separator + "nbfbFive.xlsx";
                break;
            case 6: //年报附表6
                path = "nbfbExportTemplates" + File.separator + "nbfbSix.xlsx";
                break;
            case 7: //河北省矿产资源储量表导出
                path = "nbfbExportTemplates" + File.separator + "hbskczyclbdcmb.docx";
                break;
            case 8: //河北省矿产资源储量表导出----煤炭
                path ="nbfbExportTemplates" + File.separator + "mtzyclbdcmb.docx";
                break;
            case 9: //河北省矿产资源储量表导出--目录页模版
                path ="nbfbExportTemplates" + File.separator + "clbml.docx";
                break;
            case 10: //河北省矿产资源储量表导出--最终合并模版
                path ="nbfbExportTemplates" + File.separator + "merged_template.docx";
                break;
            case 11: //年报附表7
                path ="nbfbExportTemplates" + File.separator + "nbfbSeven.xlsx";
                break;
            case 12: //年报附表8
                path ="nbfbExportTemplates" + File.separator + "nbfbEight.xlsx";
                break;

            case 13: //导出mdb模版文件
                path ="nbfbExportTemplates" + File.separator + "ks_smzq_cl.mdb";
                break;
            case 14: //导出查询模版文件
                path ="nbfbExportTemplates" + File.separator + "queryTemplate.xlsx";
                break;
            }
        return path;
    }
}